package io.renren.com.qywt.service;

import cn.hutool.core.util.ObjectUtil;
import io.renren.com.qywt.dao.StorageFeeDao;
import io.renren.com.qywt.dto.AftersaleFinanceCollectDTO;
import io.renren.com.qywt.dto.ContractDTO;
import io.renren.com.qywt.dto.MaxContractDTO;
import io.renren.com.qywt.dto.ReceiptDetailDTO;
import io.renren.com.qywt.entity.AftersaleStorageFeeEntity;
import io.renren.commons.dynamic.datasource.annotation.DataSource;
import io.renren.commons.mybatis.service.impl.BaseServiceImpl;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.enums.DeleteEnum;
import io.renren.commons.tools.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class TaskStorageFeeService extends BaseServiceImpl<StorageFeeDao,ReceiptDetailDTO>{
    @Autowired
    private AftersaleStorageFeeService aftersaleStorageFeeService;
    @Autowired
    private AftersaleFinanceCollectService aftersaleFinanceCollectService;
    @Autowired
    private TaskContractService contractService;


    @DataSource("slave1")
    @Transactional
    public List<AftersaleStorageFeeEntity> getReceiptDetail(UserDetail userDetail,AftersaleFinanceCollectDTO dto){
        Map<String,Object> params=new HashMap<>();
        Date collectDate=new Date();
        if(ObjectUtil.isNull(dto)){
            collectDate=DateUtils.addDateDays(new Date(),-1);
        }else{
            collectDate=dto.getCollectDate();
        }
        String collectDateStr=DateUtils.format(collectDate,DateUtils.DATE_PATTERN);
        params.put("collectDate",collectDateStr);
        List<ReceiptDetailDTO> receiptDetailDTOList=baseDao.getReceiptDetailList(params);
        List<AftersaleStorageFeeEntity> aftersaleStorageFeeEntityList=new ArrayList<>();
        /**
         * 临时变量
         */
        double weightFee=0.0;
        double numFee=0.0;
        double cubeFee=0.0;


        for(ReceiptDetailDTO receiptDetailDTO:receiptDetailDTOList){
            ContractDTO contractDTO = contractService.getContractBySupplierId(receiptDetailDTO.getSupplierId());
            if(ObjectUtil.isNotNull(contractDTO)) {
                if (contractDTO.getNeedMax() == 1) {//需要修正值
                    MaxContractDTO maxContractDTO = contractService.getMaxContractBySupplierId(receiptDetailDTO.getSupplierId());
                    if(ObjectUtil.isNotNull(maxContractDTO)){
                        weightFee=receiptDetailDTO.getReceivedQty()*receiptDetailDTO.getSkuWeight()*maxContractDTO.getReceiptWeight();
                        numFee=receiptDetailDTO.getReceivedQty()*maxContractDTO.getReceiptNum();
                        cubeFee=receiptDetailDTO.getReceivedQty()*(receiptDetailDTO.getSkuCube()/1000000)*maxContractDTO.getReceiptCube();
                        double max=Collections.max(Arrays.asList(weightFee,numFee,cubeFee));
                        if(max==weightFee){//按照重量计算
                            receiptDetailDTO.setRecordType(2);
                        }else if(max==numFee){//按照数量收费
                            receiptDetailDTO.setRecordType(1);
                        }else if(max==cubeFee){
                            receiptDetailDTO.setRecordType(3);
                        }
                        receiptDetailDTO.setMaxCost(max);
                    }
                }
                //体积和重量取大
                weightFee=receiptDetailDTO.getReceivedQty()*receiptDetailDTO.getSkuWeight()*contractDTO.getReceiptFee();
                cubeFee=receiptDetailDTO.getReceivedQty()*(receiptDetailDTO.getSkuCube()/1000000)*contractDTO.getReceiptCube();
                double totalCost=Collections.max(Arrays.asList(weightFee,cubeFee));
                receiptDetailDTO.setTotalCost(totalCost);
            }
            //入库费明细
            AftersaleStorageFeeEntity entity= new AftersaleStorageFeeEntity();
            entity.setBatchNo(collectDateStr.replaceAll("-",""));
            entity.setProdSku(receiptDetailDTO.getSku());
            entity.setProdName(receiptDetailDTO.getItemname());
            entity.setStorageNo(receiptDetailDTO.getReceiptKey());
            entity.setReferenceNo(receiptDetailDTO.getReferNo());
            entity.setProdFee(BigDecimal.valueOf(receiptDetailDTO.getTotalCost()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
            entity.setFare(BigDecimal.valueOf(0));
            entity.setProdNum(receiptDetailDTO.getReceivedQty());
            entity.setProdWeight(receiptDetailDTO.getSkuWeight());
            entity.setProdVolume(receiptDetailDTO.getSkuCube());
            entity.setStatus(1);
            entity.setTenantCode(userDetail.getTenantCode());
            entity.setCreator(userDetail.getId());
            entity.setDelFlag(DeleteEnum.NO.value());
            aftersaleStorageFeeEntityList.add(entity);
        }
        return aftersaleStorageFeeEntityList;
    }
}
